package com.kpx.app.wigdet;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.kpx.app.R;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.WalletFilterBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */
public class PopListSelect extends PopupWindow {
    private final ItemSelectAdapter adapter;
    private List<Object> beans;
    private View contentView;
    private ListView lvResults;

    //下拉列表项被单击的监听器
    private OnSelectClicListener listener;

    public void setData(List<Object> beans) {
        if (adapter != null) {
            this.beans = beans;
            adapter.setData(beans);
        }
    }

   public interface OnSelectClicListener {
        void onClick(Object bean);
    }

    public PopListSelect(final Context context, final List<Object> list, int width, OnSelectClicListener itemClickListener) {
        super(context);
        this.listener = itemClickListener;
        contentView = View.inflate(context, R.layout.own_dropdown_list, null);
        setContentView(contentView);
        lvResults = (ListView) contentView.findViewById(R.id.lvResults);
        beans = list;
        adapter = new ItemSelectAdapter(context, list, R.layout.item_pop_show_select);
        lvResults.setAdapter(adapter);

        //如果PopupWindow中的下拉列表项被单击了

        //则通知外部的下拉列表项单击监听器并传递当前单击项的数据
        lvResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
                if (listener != null) {
                    if (listener != null)
                        listener.onClick(beans.get(index));
                    dismiss();
                }
            }
        });

        this.setContentView(contentView);  //设置悬浮窗体内显示的内容View
        this.setWidth(width - 5);   //设置悬浮窗体的宽度
        //this.setWidth(LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);   //设置悬浮窗体的高度
        this.setBackgroundDrawable(new ColorDrawable(0x00000000)); // 设置悬浮窗体背景
        //this.setAnimationStyle(R.style.PopupAnimation);
        this.setAnimationStyle(R.anim.popup_enter);     //设置悬浮窗体出现和退出时的动画
        this.setFocusable(true);    // menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
        this.setOutsideTouchable(true);   //可以再外部点击隐藏掉PopupWindow
    }

    class ItemSelectAdapter extends ComAdapter {

        /**
         * 构造方法
         *
         * @param context  上下文对象
         * @param datas    数据
         * @param layoutId
         * @version 1.0
         * @createTime 2015年5月24日, 上午11:47:30
         * @updateTime 2015年5月24日, 上午11:47:30
         * @createAuthor chencong
         * @updateAuthor chencong
         * @updateInfo (此处输入修改内容, 若无修改可不写.)
         */
        public ItemSelectAdapter(Context context, List datas, int layoutId) {
            super(context, datas, layoutId);
        }

        @Override
        public void convert(ComViewHolder holder, Object o, int position) {
            if(o instanceof String){
                holder.setText(R.id.txt_item,(String)o);
            }else  if(o instanceof WalletFilterBean){
                holder.setText(R.id.txt_item,((WalletFilterBean) o).getText());
            }
        }
    }
}
